# syntax=docker/dockerfile:1
FROM python:3.11-slim AS base
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PATH="/app/.venv/bin:$PATH"

WORKDIR /app

# System deps for image processing and PowerPoint operations
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates curl \
    libjpeg-dev libpng-dev \
    libxml2-dev libxslt-dev \
    && rm -rf /var/lib/apt/lists/*

# Copy metadata early for layer caching
COPY pyproject.toml README.md ./

# Create venv and install
RUN python -m venv /app/.venv && \
    /app/.venv/bin/pip install --upgrade pip setuptools wheel && \
    /app/.venv/bin/pip install -e .

# Copy source
COPY src/ ./src/

# Create directory for PowerPoint files
RUN mkdir -p /app/presentations && \
    chown -R 1001:1001 /app

# Non-root user
RUN useradd -u 1001 -m appuser && chown -R 1001:1001 /app
USER 1001

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | python -m pptx_server.server | head -1 || exit 1

EXPOSE 9000
CMD ["python", "-m", "pptx_server.server"]
